package com.dp1.pucp.Seguridad.Vista;

import com.dp1.pucp.Modelo.Clases.Perfil;
import com.dp1.pucp.Util.Validaciones;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class BuscarPerfilGUI extends javax.swing.JDialog {

    public ArrayList<Perfil> listaPerfil = new ArrayList<>();
    private JFrame padre;
    private JTable tblResultados;
    private DefaultTableModel modelo;
    private JScrollPane scroll;
    private Border empty;

    public BuscarPerfilGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        this.setTitle("Busqueda de Perfiles");
    }

    public JFrame getPadre() {
        return padre;
    }

    public String getNombre() {
        return txtNombre.getText().trim();
    }

    public int getFilaSel() {
        if (tblResultados != null) {
            return tblResultados.getSelectedRow();
        } else {
            return -1;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnBuscar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblEstado = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        btnRefrescar = new javax.swing.JButton();
        btnSeleccionar = new javax.swing.JButton();
        panelTabla = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Empleado");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        btnBuscar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/buscar.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.setBorderPainted(false);
        btnBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnCancelar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setBorderPainted(false);
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        lblEstado.setText("Nombre:");

        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombreKeyTyped(evt);
            }
        });

        btnRefrescar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnRefrescar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/clear.png"))); // NOI18N
        btnRefrescar.setText("Limpiar");
        btnRefrescar.setBorderPainted(false);
        btnRefrescar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRefrescar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRefrescar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefrescarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblEstado)
                        .addGap(58, 58, 58)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(59, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRefrescar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar)
                        .addGap(108, 108, 108))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEstado))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnRefrescar)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        btnSeleccionar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnSeleccionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/checkmark.png"))); // NOI18N
        btnSeleccionar.setText("Seleccionar");

        panelTabla.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelTabla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSeleccionar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSeleccionar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRefrescarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefrescarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnRefrescarActionPerformed

    private void txtNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyTyped
        // TODO add your handling code here:
        String car = txtNombre.getText();
        if (car.length() >= 50) {
            evt.consume();
        }
        Validaciones.soloTexto(evt);
    }//GEN-LAST:event_txtNombreKeyTyped

    public void LimpiarDatos() {
        txtNombre.setText("");

        panelTabla.removeAll();
        panelTabla.setBorder(empty);
        panelTabla.updateUI();
    }

    public void addBuscarListener(ActionListener ml) {
        this.btnBuscar.addActionListener(ml);
    }

    public void addCancelarListener(ActionListener ml) {
        this.btnCancelar.addActionListener(ml);
    }

    public void addLimpiarListener(ActionListener ml) {
        this.btnRefrescar.addActionListener(ml);
    }

    public void addSeleccionarListener(ActionListener ml) {
        this.btnSeleccionar.addActionListener(ml);
    }

    public void llenaGrilla(ArrayList<Perfil> listPerfil) {
        panelTabla.removeAll();
        TitledBorder tituloResultado = BorderFactory.createTitledBorder(empty, "Resultado:");
        tituloResultado.setTitleJustification(TitledBorder.LEFT);
        panelTabla.setBorder(BorderFactory.createTitledBorder("Resultados"));
        panelTabla.updateUI();
        if (listPerfil != null) {
            if (!listPerfil.isEmpty()) {
                modelo = new DefaultTableModel() {
                    @Override
                    public boolean isCellEditable(int fila, int columna) {
                        return false;
                    }
                };

                modelo.setRowCount(listPerfil.size());
                Object nombreCols[] = new Object[2];
                nombreCols[0] = "";
                nombreCols[1] = "Nombre";
                modelo.setColumnIdentifiers(nombreCols);


                for (int i = 0; i < listPerfil.size(); i++) {
                    if (i < 9) {
                        modelo.setValueAt("0" + (i + 1), i, 0);
                    } else {
                        modelo.setValueAt(i + 1, i, 0);
                    }
                    modelo.setValueAt(listPerfil.get(i).getNombre(), i, 1);
                }


                tblResultados = new JTable();
                tblResultados.setDragEnabled(false);
                tblResultados.setModel(modelo);
                TableColumn tcol = tblResultados.getColumn("");
                tcol.setPreferredWidth(30);
                tcol.setMinWidth(30);
                tcol.setMaxWidth(30);
                tblResultados.getColumnModel().getColumn(0).setCellRenderer(tblResultados.getTableHeader().getDefaultRenderer());
                tblResultados.setEnabled(true);
                tblResultados.setRowSelectionAllowed(true);
                scroll = new JScrollPane(tblResultados);
                panelTabla.add(scroll);

            } else {
                Validaciones.mostrarMensajeAvisoVal("No se encontraron resultados.");
            }
        } else {
            Validaciones.mostrarMensajeAvisoVal("No se encontraron resultados.");
        }

        panelTabla.updateUI();
    }

//    public void llenaGrilla(ArrayList<Perfil> lista) {
//        if (lista != null) {
//            ((DefaultTableModel) this.tblEmpleado.getModel()).setRowCount(0);
//            for (int i = 0; i < lista.size(); i++) {
//                ((DefaultTableModel) tblEmpleado.getModel()).setRowCount(tblEmpleado.getRowCount() + 1);
//                tblEmpleado.setValueAt(lista.get(i).getNombre(), i, 0);
//            }
//        }
//    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRefrescar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JPanel panelTabla;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
